﻿<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.util.Date" %>
<%@ page import="javax.swing.*" %>
<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>登录程序之登录成功页面</title>
</head>
<body>
<center>
    <h2>登录成功！欢迎[<%=session.getAttribute("username")%>]访问新闻管理系统！</h2>
    <hr>
    <a href="Insert.jsp">增加新闻</a>&nbsp;&nbsp;
    <a href="logout.jsp">安全退出</a>
    <%
        request.setCharacterEncoding("UTF-8");
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String title = request.getParameter("标题");
        String auther = request.getParameter("作者");
        String date = request.getParameter("日期");
        String heat = request.getParameter("热度");
        int result = 0;
        boolean isError = false;

        try {

            //加载数据驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_news2019", "root", "123456");
            if (title != null) {
                stmt = conn.prepareStatement("insert t_news values (?,?,?,?)");
                stmt.setObject(1, title);
                stmt.setObject(2, auther);
                stmt.setObject(3, date);
                stmt.setObject(4, Integer.parseInt(heat));
                result = stmt.executeUpdate();
            }
            //创建语句容器
            stmt = conn.prepareStatement("select * from t_news");
            rs = stmt.executeQuery();

    %>

    <table align="center" border="1" cellspacing="0">
        <tr>
            <th>标题</th>
            <th>作者</th>
            <th>日期</th>
            <th>热度</th>
            <th>操作</th>
        </tr>
        <%
            while (rs.next()) {
        %>
        <tr>
            <td><%=rs.getObject("标题")%>
            </td>
            <td><%=rs.getObject("作者")%>
            </td>
            <td><%=rs.getObject("日期")%>
            </td>
            <td><%=rs.getObject("热度")%>
            </td>
            <td>
                <a href="">查询详细</a>&nbsp;
                <a href="">修改</a>&nbsp;
                <a href="">删除</a>
            </td>
        </tr>
        <%
                }
                rs.close();
                stmt.close();
                conn.close();
            } catch (Exception e) {
                isError = true;
            }
        %>
    </table>
</center>
</body>
<script>
    <%
    if (isError) {
    %>
    alert("新闻标题为空或该新闻标题已存在！");
    window.location.href = "login_success.jsp";
    <%
    }
    %>
</script>
</html>

